
module CommandLine where
import DataUpdate
import Synchronizers
import System
import System.IO
import Data.Char (isSpace)
import DataParser
import UpdateParser



--------------------------------------------------------------------------------
-- Command Line

trim      :: String -> String
trim      = f . f
   where f = reverse . dropWhile isSpace

run c args = do
    args <- return $ map trim args
    showHelp <- return $ any (=="--help") args || any (=="-h") args
    showUpdatedValue <- return $ any (=="--updatedValues") args || any (=="-v") args
    showfullUpdates <- return $ any (=="--fullUpdates") args || any (=="-f") args
    showPrompt <- return $ not (any (=="--silentMode") args || any (=="-s") args)
    if showHelp
        then putStrLn helpMessage
        else do
            printLnIf showPrompt "Please input the values:"
            valueString <- getLine
            printLnIf showPrompt "Please input the updates:"
            updateString <- getLine
            case parseString2ValEnv valueString of
                  Left message -> putStrLn  ("Value format error:" ++ message)
                  Right values -> 
                    case parseString2UpdateEnv updateString of
                            Left message -> putStrLn $ "Update format error:" ++ message
                            Right updates ->
                                case sync c values updates of
                                      Nothing -> putStrLn  "Synchronization failed."
                                      Just outUpdates -> do
                                        if showfullUpdates
                                            then do
                                                printIf showPrompt "output updates: "
                                                putStrLn $ show outUpdates
                                            else do
                                                printIf showPrompt "output updates: "
                                                putStrLn $ show (effectiveEnv outUpdates values)
                                        if showUpdatedValue 
                                             then do
                                                printIf showPrompt "output values:  "
                                                putStrLn $ show (applyEnv outUpdates values)
                                             else 
                                                return ()
    where
        printLnIf shouldprint msg = if shouldprint then putStrLn msg else return () 
        printIf shouldprint msg = if shouldprint then putStr msg else return () 
     
helpMessage = "\
\Options:\n\
\   --help or -h: This help message\n\
\   --updatedValues or -v: show the result of applying the output update to input values\n\
\   --fullUpdates or -f: show all updates produced even if it does not changes values\n\
\   --sildentMode or -s: print no prompt message(suitable for analysis by programs)"